<?php if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Returns extends CI_Controller
{

    function returns()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->library('form_validation');
        $this->load->library('encrypt');
        $this->load->library('enc');
        $this->load->model('mmaster');
        $this->load->model('mservices');
        $this->load->model('mterritory');
        $this->load->model('mpage');
        $this->load->model('mreturns');
        $this->load->helper('url');
		$this->load->helper('file');
		$this->load->library('fpdf');
    }

    function is_logged_in()
    {
        $is_logged_in = $this->session->userdata('is_logged_in');
        if (!isset($is_logged_in) || $is_logged_in != true) {
            redirect(base_url() . 'index.php/login');
        }
    }


    function index()
    {
        $this->create();
    }
	
	function save()
    {
		$entry = $this->input->post("jumlah_entry");
		$check = false;
		for($i=1; $i<=$entry; $i++){
			if($this->input->post("nomorBarang".$i)){
				//$this->form_validation->set_rules('territory_id', 'territory id', 'required');
				//$this->form_validation->set_rules('return_date', 'return date', 'required');
				//$this->form_validation->set_rules('return_no', 'return no', 'required');
				$this->form_validation->set_rules('sales_id', 'sales id', 'required');
				$this->form_validation->set_rules('outlet_id', 'outlet id', 'required');
				$this->form_validation->set_rules('sales_name', 'sales name', 'required');
				$this->form_validation->set_rules('outlet_name', 'outlet name', 'required');
				//$this->form_validation->set_rules('nomorBarang'.$entry, 'iccid', 'required');
				//$this->form_validation->set_rules('item_name[]', 'nama item', 'required');
				//$this->form_validation->set_rules('price[]', 'price', 'required|numeric');
				
				if ($this->form_validation->run() != false) {
					//alert("jkjkjk");
					$return_date = $this->input->post("return_date");
					$sales_id = $this->input->post("sales_id");
					$outlet_id = $this->input->post("outlet_id");
					
					//$status = 1;
					$remark = '';
					if ($this->input->post("remark")){
						$remark = $this->input->post("remark");
					}
					
					$salesOrder = $this->mreturns->saveReturn($return_date, $sales_id, $outlet_id, $remark);
					$return_id = $salesOrder->return_id;
					
					//$outlet_id = $this->input->post("outlet_id");
					//$sales_id = $this->input->post("sales_id");
					
					//$dateambil = $this->input->post("return_date");
					$date = new DateTime($return_date);
					$date_id = $date->format('Ymd');
					
					$buy = "buying";
					$id = "nomorBarang";
					$cost = "price";
					$name = "desc";
					
					$file = "file".DIRECTORY_SEPARATOR."integration".DIRECTORY_SEPARATOR."return".DIRECTORY_SEPARATOR."out".DIRECTORY_SEPARATOR.$date_id."_".$return_id.".txt";
					$data = "return_id,outlet_id,sales_id,iccid,item_code,is_valid\r\n";
					
					for($i=1; $i<=$entry; $i++){
						//$buying_date = $this->input->post($buy.$i);
						$iccid = $this->input->post($id.$i);
						//$price = $this->input->post($cost.$i);
						//$itemname = $this->input->post($name.$i);
						
						if($iccid){
							//$this->mreturns->saveReturnDetail($return_id, $iccid, $outlet_id, $sales_id);
							$append = $this->mreturns->saveReturnDetail($return_id, $iccid, $outlet_id, $sales_id);
							$data = $data.$append;
						}
					}
					
					write_file($file, $data);
					$this->session->set_flashdata('message', 'Data Berhasil dimasukkan');
					redirect(base_url() . "index.php/returns");
				}
				else{
					$this->session->set_flashdata('message', 'Data Tidak Berhasil dimasukkan');
					redirect(base_url() . "index.php/returns");
				}
				$i=$entry+1;
				$check = true;
			}
		}
		if(!$check){
			$this->session->set_flashdata('message', 'Tidak Ada Barang Yang Di Return');
			redirect(base_url() . "index.php/returns");
		}
		
		
    }
	
	// function saveSwap()
 //    {
	// 	$entry = $this->input->post("jumlah_entry");
	// 	$return_no = $this->input->post("return_no");
	// 	//$return_id = $this->input->post("return_no");
	// 	$return_id = intval($return_no);
	// 	$check = false;

	// 	for($i=1; $i<=$entry; $i++){
	// 		if($this->input->post("iccid".$i)){
	// 			$this->form_validation->set_rules('return_no', 'return no', 'required');
				
	// 			if ($this->form_validation->run() != false) {
	// 				$id = "iccid";
	// 				for($j=$i; $j<=$entry; $j++){
	// 					$iccid = $this->input->post($id.$j);
						
	// 					if($iccid){
	// 						$this->mreturns->saveSwapDetail($return_id, $iccid);
	// 					}
	// 				}
					
	// 				$this->session->set_flashdata('message', 'Data Berhasil dimasukkan');
	// 				redirect(base_url() . "index.php/returns");
	// 			}
	// 			/*else{
	// 				$this->session->set_flashdata('message', 'Data Tidak Berhasil dimasukkan');
	// 				redirect(base_url() . "index.php/returns");
	// 			}*/
	// 			$i=$entry+1;
	// 			$check = true;
	// 		}
	// 	}
	// 	if(!$check){
	// 		$this->session->set_flashdata('message', 'Tidak Ada Barang Yang Di Swap');
	// 		redirect(base_url() . "index.php/returns/returnSwapDetail/".$return_id);
	// 	}
 //    }

    public function saveSwap()
    {
    	$return_id = intval($this->input->post("return_no"));

    	//encode return id dari f-returns-swap-detail
    	$return_id = $this->enc->fogit($return_id);

    	$items = $this->input->post("iccid");
    	$this->form_validation->set_rules('iccid[]', 'return no', 'required');
		if ($this->form_validation->run() != false) {
	    	foreach ($items as $item) {
	    		$this->mreturns->saveSwapDetail($return_id, $item);
			}
			$this->session->set_flashdata('message', 'Data Berhasil dimasukkan');
			redirect(base_url() . "index.php/returns");
		}
		else {
			$this->session->set_flashdata('message', 'Tidak Ada Barang Yang Di Swap');
			redirect(base_url() . "index.php/returns/returnSwapDetail/".$return_id);
		}

    }
	
	function getOutlet($id){
		/*if('IS_AJAX') {
			//$data['outlet'] = $this->mreturns->getOutlet();
			$outlet = $this->mreturns->getOutlet($sales_id);
			
			foreach($outlet as $item){
				echo "<option value=\"".$item->outlet_id."\">".$item->outlet_name."</option>";
			}
			//$this->load->view('outlet',$data);
		}*/
		//$id = $this->input->post('sales');
		$data['outlet'] = $this->mreturns->getOutlet($id);
		if('IS_AJAX')
        {
            echo json_encode($data['outlet']); //echo json string if ajax request
 
        }
        //$this->load->view('outlet',$data);
	}
	
    function create($proses = "add", $id = "")
    {
        $data['template'] = "shell/smooth";
		$data['main_view'] = "f-returns";
        $data['jenis'] = "create";
        $data['proses'] = $proses;
		$data['judul'] = "Baru -  Nota Pengembalian";
        $data['id'] = $id;
		
		$territory = $this->mreturns->getTerritoryId();
		$territory_id = $territory->territory_id;
		
		$data['cluster'] = $this->mreturns->getCluster();
		//$return_no = $this->mreturns->getReturnNo();
        //$data['return_no'] = $return_no->return_id;
        $data['sales'] = $this->mreturns->getSales();
		//$data['sales'] = $this->mreturns->getSales($territory_id);
		// $data['itemname'] = $this->mreturns->getItemName();
		$data['itemname'] = $this->mreturns->getPrice();
		$data['itemprice'] = $this->mreturns->getPrice();
		$data['buyingdate'] = $this->mreturns->getBuyingDate();

			if($this->input->post("iccid")){
				$this->form_validation->set_rules('sales_id', 'sales id', 'required');
				$this->form_validation->set_rules('outlet_id', 'outlet id', 'required');
				$this->form_validation->set_rules('sales_name', 'sales name', 'required');
				$this->form_validation->set_rules('outlet_name', 'outlet name', 'required');
				

				if ($this->form_validation->run() != false) {
					$return_date = $this->input->post("return_date");
					$sales_id = $this->input->post("sales_id");
					$outlet_id = $this->input->post("outlet_id");
					$iccid = $this->input->post('iccid');
					
					$remark = '';
					if ($this->input->post("remark")){
						$remark = $this->input->post("remark");
					}
					
					$salesOrder = $this->mreturns->saveReturn($return_date, $sales_id, $outlet_id, $remark);
					$return_id  = $salesOrder->return_id;
					foreach ($iccid as $key) {
						$this->mreturns->saveReturnDetail($key, $return_id);
					}

					// return (print_r($iccid));


					/********************** Bagian ini menyimpan ke file **********************/
					// $return_id = $salesOrder->return_id;
					
					// $date = new DateTime($return_date);
					// $date_id = $date->format('Ymd');
					
					// $buy = "buying";
					// $id = "nomorBarang";
					// $cost = "price";
					// $name = "desc";
					
					// $file = "file".DIRECTORY_SEPARATOR."integration".DIRECTORY_SEPARATOR."return".DIRECTORY_SEPARATOR."out".DIRECTORY_SEPARATOR.$date_id."_".$return_id.".txt";
					// $data = "return_id,outlet_id,sales_id,iccid,item_code,is_valid\r\n";
					
					// for($j=$i; $j<=$entry; $j++){
					// 	$iccid = $this->input->post($id.$j);
						
					// 	if($iccid){
					// 		$append = $this->mreturns->saveReturnDetail($return_id, $iccid, $outlet_id, $sales_id);
					// 		$data = $data.$append;
					// 	}
					// }
					
					// write_file($file, $data);
					/*********************************** END ***********************************/
					$this->session->set_flashdata('message', 'Data Berhasil dimasukkan');
					redirect(base_url() . "index.php/returns/returnOutstanding");
				}
				
			}

		$hasil = "";

        $this->load->view($data['template'], $data);
		//if(!$check){
		//	$this->session->set_flashdata('message', 'Tidak Ada Barang Yang Di Return');
		//	redirect(base_url() . "index.php/returns");
		//}
		
        
    }

    
    function getRelatedIccid($testValue){ // Untuk fitur auto komplit
        $data['relatedIccid'] = $this->mreturns->getRelatedIccid($testValue); //Search DB
        
        if('IS_AJAX') 
        	{ echo json_encode($data['relatedIccid']); }
    }
    function getReturnItem($iccid){ // Untuk fitur auto komplit
        $data['returnItems'] = $this->mreturns->getItemSold($iccid); //Search DB
        
        if('IS_AJAX') 
        	{ echo json_encode($data['returnItems']); }
    }

    function getSwapItemCandidate($iccid)
    {
		$data['swapitem'] = $this->mreturns->getSwapItemCandidate($iccid);
		echo json_encode($data['swapitem']);;
    }
    function getSwapItem($iccid){ // Untuk fitur auto komplit
        $data['returnItems'] = $this->mreturns->getSwapItem($iccid); //Search DB
        
        if('IS_AJAX') 
        	{ echo json_encode($data['returnItems']); }
    }
	function returnOutstanding($act="",$page="")
	{
		$data['template'] = "shell/smooth";
		$data['main_view']     = 'f-return-outstanding';
		$this->load->view($data['template'],$data);
	}
	
	function returnSwap($act="",$page="")
	{
		$data['template'] = "shell/smooth";
		$data['main_view']     = 'f-return-swap';
		$this->load->view($data['template'],$data);
	}
	
	function returnAuthorized($act="",$page="")
	{
		$data['template'] = "shell/smooth";
		$data['main_view']     = 'f-return-authorized';
		$this->load->view($data['template'],$data);
	}
	
	function getReturnNoString($return_id)
	{
		$temp = "".$return_id;
		if(strlen($temp) < 8){
			for($i=strlen($temp); $i<8; $i++){
				$temp = "0".$temp;
			}
		}
		return $temp;
	}
	
	function approve($return_id) {
		// $data['template']  = "shell/smooth";
		// $data['main_view'] = 'f-return-swap';

		$data['return_detail_id']= $this->input->post("validReturn");
		$data['return_detail_iccid']= $this->input->post("iccid");

		$update_return_detail = $this->mreturns->saveReturnOutstanding($return_id, $data);

		redirect('index.php/returns/returnSwap');
		// $this->load->view($data['template'],$data);
	}

	function returnOutstandingDetail($return_id, $param='')
    {
    	$return_id = $this->enc->defog($return_id);
        $data['template'] = "shell/smooth";
		$data['main_view'] = "f-returns";
        $data['jenis'] = "listOutstanding";
        $data['proses'] = "";
		$data['judul'] = "Return Outstanding Detail";
        //$data['id'] = $id;
		
		$returns = $this->mreturns->getReturn($return_id);
		$sales_id = $returns->user_id;
		$outlet_id = $returns->channel_id;
		$sales_name = $this->mreturns->getSalesName($sales_id);


		$data['return'] = $this->mreturns->returned($return_id);
		$data['items'] = $this->mreturns->returnedItems($return_id);
		// $outlet_name = $this->mreturns->getOutletName($outlet_id);
		
		$data['cluster'] = $this->mreturns->getCluster();
		// $no = substr("00000000".$return_id,-8);
		// $data['return_date'] = $returns->return_date;
		// //$data['returns_no'] = $return_id;
		// $data['returns_no'] = $no;
		// $data['sales_id'] = $sales_id;
		// $data['sales_name'] = $sales_name->user_name;
		// $data['outlet_id'] = $outlet_id;
		// $data['outlet_name'] = $outlet_name->outlet_name;
		// $data['remarks'] = $returns->remark;
		// $data['return_id'] = $return_id;
  //       $data['items'] = $this->mreturns->getItems($return_id);
		// $data['itemname'] = $this->mreturns->getItemName();
		// $data['itemprice'] = $this->mreturns->getPrice();
		// $data['buyingdate'] = $this->mreturns->getBuyingDateDetail();
  //       $hasil = "";
        	
        $this->load->view($data['template'], $data);
    }
	
	function returnSwapDetail($return_id)
    {
		$return_id = $this->enc->defog($return_id);
        $data['template'] = "shell/smooth";
		$data['main_view'] = "f-returns";
        $data['jenis'] = "listSwap";
        $data['proses'] = "";
		$data['judul'] = "Return Swap Detail";
	
		$data['returns'] = $this->mreturns->getReturn2($return_id);

		$data['cluster'] = $this->mreturns->getCluster();
		$data['items'] = $this->mreturns->returnedItems($return_id);



        $this->load->view($data['template'], $data);
    }
	
	function returnSuccessDetail($return_id)
    {
    	$return_id = $this->enc->defog($return_id);
        $data['template'] = "shell/smooth";
		$data['main_view'] = "f-returns";
        $data['jenis'] = "listSuccess";
        $data['proses'] = "";
		$data['judul'] = "Return Authorized Detail";
        //$data['id'] = $id;
		$data['cluster'] = $this->mreturns->getCluster();
		$data['return'] = $this->mreturns->returned($return_id);
		$data['items'] = $this->mreturns->returnedItems($return_id);
		$data['swap'] = $this->mreturns->getItemsSwap($return_id);
		// $returns = $this->mreturns->getReturn($return_id);
		// $sales_id = $returns->user_id;
		// $outlet_id = $returns->channel_id;
		// $sales_name = $this->mreturns->getSalesName($sales_id);
		// $outlet_name = $this->mreturns->getOutletName($outlet_id);
		
		// $no = substr("00000000".$return_id,-8);
		// $data['cluster'] = $this->mreturns->getCluster();
		// $data['return_date'] = $returns->return_date;
		// //$data['returns_no'] = $return_id;
		// $data['returns_no'] = $no;
		// $data['sales_id'] = $sales_id;
		// $data['sales_name'] = $sales_name->user_name;
		// $data['outlet_id'] = $outlet_id;
		// $data['outlet_name'] = $outlet_name->outlet_name;
		// $data['remarks'] = $returns->remark;
		
  //       $data['items'] = $this->mreturns->getItems($return_id);
		// $data['itemname'] = $this->mreturns->getItemName();
		// $data['itemprice'] = $this->mreturns->getPrice();
		// $data['buyingdate'] = $this->mreturns->getBuyingDateDetail();
		// $data['swap'] = $this->mreturns->getItemsSwap($return_id);
		
        // $hasil = "";

        $this->load->view($data['template'], $data);
    }
	
	public function readOutstanding() {
		//echo json_encode( $this->mreturns->getAllOutstanding() );
		$result = $this->mreturns->getAllOutstanding();

        //encode
        for($i=0;$i<count($result);$i++){
    		$result[$i]->return_id = $this->enc->fogit($result[$i]->return_id);
    	}
        
        echo json_encode($result);
	}
	
	public function readSwap() {
		//echo json_encode( $this->mreturns->getAllSwap() );
		$result = $this->mreturns->getAllSwap();

        //encode
        for($i=0;$i<count($result);$i++){
    		$result[$i]->return_id = $this->enc->fogit($result[$i]->return_id);
    	}
        
        echo json_encode($result);
	}
	
	public function readSuccess() {
		//echo json_encode( $this->mreturns->getAllSuccess() );
		$result = $this->mreturns->getAllSuccess();

        //encode
        for($i=0;$i<count($result);$i++){
    		$result[$i]->return_id = $this->enc->fogit($result[$i]->return_id);
    	}
        
        echo json_encode($result);

	}
	
	function defoger()
    {
        $result = $this->enc->defog($this->input->post('str'));
        echo $result;  
    }

	function printOutstanding($return_id)
    {
    	$return_id = $this->enc->defog($return_id);
        $no = substr("00000000".$return_id,-8);
		$returns = $this->mreturns->getReturn($return_id);
		$sales_id = $returns->user_id;
		$outlet_id = $returns->channel_id;
		$sales_name = $this->mreturns->getSalesName($sales_id);
		$outlet_name = $this->mreturns->getOutletName($outlet_id);
		
		$datacluster = $this->mreturns->getCluster();
		$cluster = "";
		foreach($datacluster as $item){
			$cluster = $item->territory_name;
		}
		$return_date = $returns->return_date;
		//$data['returns_no'] = $return_id;
		//$data['sales_id'] = $sales_id;
		$nama_sales = $sales_name->user_name;
		//$data['outlet_id'] = $outlet_id;
		$nama_outlet = $outlet_name->outlet_name;
		//$data['remarks'] = $returns->remark;
		
        $iccid_valid = $this->mreturns->getItemsOutstandingPrint($return_id);
		$i = 0;
		$iccid = array();
		$nama = array();
		$price = array();
		$tgl_beli = array();
		
		//begin configuration
		$textColour = array( 0, 0, 0 );
		$headerColour = array( 100, 100, 100 );
		$tableHeaderTopTextColour = array( 255, 255, 255 );
		$tableHeaderTopFillColour = array( 125, 152, 179 );
		$tableHeaderTopProductTextColour = array( 0, 0, 0 );
		$tableHeaderTopProductFillColour = array( 143, 173, 204 );
		$tableHeaderLeftTextColour = array( 99, 42, 57 );
		$tableHeaderLeftFillColour = array( 184, 207, 229 );
		$tableBorderColour = array( 50, 50, 50 );
		$tableRowFillColour = array( 213, 170, 170 );
		
		$this->load->helper('path');

		$font_directory = './fpdf_fonts/';
		set_realpath($font_directory);
		define('FPDF_FONTPATH',$font_directory);
		
		$this->fpdf->Open();
		$this->fpdf->AddPage();
		$this->fpdf->SetAutoPageBreak(true, 20);
		$this->fpdf->SetMargins(20, 20, 20);
		$this->fpdf->AliasNbPages();
		
		//header
		$this->fpdf->Image(FCPATH.'file/images/logo_smartfren.png',10,3,50,0,'');		
		$this->fpdf->SetFont('Arial','B',16);
		$this->fpdf->Cell(179,-5,"Return Outstanding Notes",0,1,'R');	
		$this->fpdf->Ln( 10 );		
		$this->fpdf->Cell(166,0,"(Pengembalian Barang)",0,1,'R');		
		$this->fpdf->Line(10, 20, 200, 20);
		
		//table
		$this->fpdf->SetDrawColor( $tableBorderColour[0], $tableBorderColour[1], $tableBorderColour[2] );
		
		//Cluster Information		
		$this->fpdf->Ln(5);
		$this->fpdf->SetFont('Arial','B',16);		
		$this->fpdf->Write(19, $cluster);
		//$this->fpdf->Ln( 6 );
		//$this->fpdf->SetFont('Helvetica','',10);
		//$this->fpdf->Write(19, $dist[0]['distributor_address']);
		
		// Date and receipt no
		$this->fpdf->Ln(15);
		$this->fpdf->SetXY(130, 42);
		$this->fpdf->SetFont('Arial','B',8);
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->Cell( 30, 8, "Date", 1, 0, 'C', true );
		$this->fpdf->Cell( 35, 8, "Return No", 1, 0, 'C', true );		
		//$this->fpdf->Ln( 8 );
		$this->fpdf->SetXY(130, 50);
		
		$fill = false;
		$row = 0;		
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->SetFont( 'Arial', '', 8 );		
		$this->fpdf->Cell( 30, 8, $return_date, 1, 0, 'C', $fill );
		//$this->fpdf->Cell( 25, 8, date("d-m-Y", strtotime($return_date)), 1, 0, 'C', $fill );
		$this->fpdf->Cell( 35, 8, $no, 1, 0, 'C', $fill );
					
		
		$this->fpdf->Ln( 20 );
		
		// Order Information
		$this->fpdf->SetXY(20, 42);
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );
		$this->fpdf->Cell( 25, 8, "Sales ID", 1, 0, 'C', true );
		$this->fpdf->Cell( 45, 8, "Sales Name", 1, 0, 'C', true );		
		$this->fpdf->Ln( 8 );
		$this->fpdf->SetFont( 'Arial', '', 8 );
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->Cell( 25, 8, $sales_id, 1, 0, 'C', $fill );
		$this->fpdf->Cell( 45, 8, $nama_sales, 1, 0, 'C', $fill );

		$this->fpdf->Ln( 25 );
		$this->fpdf->SetXY(20, 62);
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );
		$this->fpdf->Cell( 25, 8, "Outlet ID", 1, 0, 'C', true );
		$this->fpdf->Cell( 45, 8, "Outlet Name", 1, 0, 'C', true );		
		$this->fpdf->Ln( 8 );
		$this->fpdf->SetFont( 'Arial', '', 8 );
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->Cell( 25, 8, $outlet_id, 1, 0, 'C', $fill );
		$this->fpdf->Cell( 45, 8, $nama_outlet, 1, 0, 'C', $fill );
		
		$this->fpdf->Ln(10);
		$this->fpdf->SetFont('Arial','B',16);		
		$this->fpdf->Write(15, "Return Product");
		
		// Order Information
		$this->fpdf->Ln( 15 );
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );
		$this->fpdf->Cell( 40, 8, "ICCID", 1, 0, 'C', true );
		$this->fpdf->Cell( 30, 8, "Buying Date", 1, 0, 'C', true );		
		$this->fpdf->Cell( 80, 8, "Description", 1, 0, 'C', true );		
		$this->fpdf->Cell( 25, 8, "Price", 1, 0, 'C', true );			
		$this->fpdf->SetFont( 'Arial', '', 8 );
		
		foreach($iccid_valid as $cek){
			$cekid = $cek->iccid;
			$iccid = $cekid;
			$nama = $this->mreturns->getSwapPrintNama($cekid);
			$price = $this->mreturns->getSwapPrintPrice($cekid);
			//$tgl_beli = $this->mreturns->getSwapPrintDate($cekid);
			$str = $this->mreturns->getSwapPrintDate($cekid);
			$tgl_beli = substr($str, 0, 10);
			//$i++;
			
			$this->fpdf->Ln( 8 );
			$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
			$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
			$this->fpdf->Cell( 40, 8, $iccid, 1, 0, 'C', $fill );
			$this->fpdf->Cell( 30, 8, $tgl_beli, 1, 0, 'C', $fill );		
			$this->fpdf->Cell( 80, 8, $nama, 1, 0, 'C', $fill );		
			$this->fpdf->Cell( 25, 8, $price, 1, 0, 'C', $fill );	
		}
		
		  //Position at 1.5 cm from bottom 
        $this->fpdf->SetY(-30); 
        //Arial italic 8 
        $this->fpdf->SetFont('Arial','I',8); 
        //Page number 
        $this->fpdf->Cell(0,10,'Page '.$this->fpdf->PageNo().'/{nb}',0,0,'C'); 
		
		
		$this->fpdf->Output("Return_Outstanding_Note_".$no.".pdf", "I");
		
    }
	
	function printSwap($return_id)
    {
    	$return_id = $this->enc->defog($return_id);
        $no = substr("00000000".$return_id,-8);
		$returns = $this->mreturns->getReturn($return_id);
		$sales_id = $returns->user_id;
		$outlet_id = $returns->channel_id;
		$sales_name = $this->mreturns->getSalesName($sales_id);
		$outlet_name = $this->mreturns->getOutletName($outlet_id);
		
		$datacluster = $this->mreturns->getCluster();
		$cluster = "";
		foreach($datacluster as $item){
			$cluster = $item->territory_name;
		}
		$return_date = $returns->return_date;
		//$data['returns_no'] = $return_id;
		//$data['sales_id'] = $sales_id;
		$nama_sales = $sales_name->user_name;
		//$data['outlet_id'] = $outlet_id;
		$nama_outlet = $outlet_name->outlet_name;
		//$data['remarks'] = $returns->remark;
		
        $iccid_valid = $this->mreturns->getItemsSwapPrint($return_id);
		$i = 0;
		$iccid = array();
		$nama = array();
		$price = array();
		$tgl_beli = array();
		$state = array();
		
		//begin configuration
		$textColour = array( 0, 0, 0 );
		$headerColour = array( 100, 100, 100 );
		$tableHeaderTopTextColour = array( 255, 255, 255 );
		$tableHeaderTopFillColour = array( 125, 152, 179 );
		$tableHeaderTopProductTextColour = array( 0, 0, 0 );
		$tableHeaderTopProductFillColour = array( 143, 173, 204 );
		$tableHeaderLeftTextColour = array( 99, 42, 57 );
		$tableHeaderLeftFillColour = array( 184, 207, 229 );
		$tableBorderColour = array( 50, 50, 50 );
		$tableRowFillColour = array( 213, 170, 170 );
		
		$this->load->helper('path');

		$font_directory = './fpdf_fonts/';
		set_realpath($font_directory);
		define('FPDF_FONTPATH',$font_directory);
		
		$this->fpdf->Open();
		$this->fpdf->AddPage();
		$this->fpdf->SetAutoPageBreak(true, 20);
		$this->fpdf->SetMargins(20, 20, 20);
		$this->fpdf->AliasNbPages();
		
		//header
		$this->fpdf->Image(FCPATH.'file/images/logo_smartfren.png',10,3,50,0,'');		
		$this->fpdf->SetFont('Arial','B',16);
		$this->fpdf->Cell(169,-5,"Return Swap Notes",0,1,'R');	
		$this->fpdf->Ln( 10 );		
		$this->fpdf->Cell(165,0,"(Pengembalian Barang)",0,1,'R');		
		$this->fpdf->Line(10, 20, 200, 20);
		
		//table
		$this->fpdf->SetDrawColor( $tableBorderColour[0], $tableBorderColour[1], $tableBorderColour[2] );
		
		//Cluster Information		
		$this->fpdf->Ln(5);
		$this->fpdf->SetFont('Arial','B',16);		
		$this->fpdf->Write(19, $cluster);
		//$this->fpdf->Ln( 6 );
		//$this->fpdf->SetFont('Helvetica','',10);
		//$this->fpdf->Write(19, $dist[0]['distributor_address']);
		
		// Date and receipt no
		$this->fpdf->Ln(15);
		$this->fpdf->SetXY(130, 42);
		$this->fpdf->SetFont('Arial','B',8);
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->Cell( 30, 8, "Date", 1, 0, 'C', true );
		$this->fpdf->Cell( 35, 8, "Return No", 1, 0, 'C', true );		
		//$this->fpdf->Ln( 8 );
		$this->fpdf->SetXY(130, 50);
		
		$fill = false;
		$row = 0;		
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->SetFont( 'Arial', '', 8 );		
		$this->fpdf->Cell( 30, 8, $return_date, 1, 0, 'C', $fill );
		//$this->fpdf->Cell( 25, 8, date("d-m-Y", strtotime($return_date)), 1, 0, 'C', $fill );
		$this->fpdf->Cell( 35, 8, $no, 1, 0, 'C', $fill );
					
		
		$this->fpdf->Ln( 20 );
		
		// Order Information
		$this->fpdf->SetXY(20, 42);
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );
		$this->fpdf->Cell( 25, 8, "Sales ID", 1, 0, 'C', true );
		$this->fpdf->Cell( 45, 8, "Sales Name", 1, 0, 'C', true );		
		$this->fpdf->Ln( 8 );
		$this->fpdf->SetFont( 'Arial', '', 8 );
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->Cell( 25, 8, $sales_id, 1, 0, 'C', $fill );
		$this->fpdf->Cell( 45, 8, $nama_sales, 1, 0, 'C', $fill );

		$this->fpdf->Ln( 25 );
		$this->fpdf->SetXY(20, 62);
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );
		$this->fpdf->Cell( 25, 8, "Outlet ID", 1, 0, 'C', true );
		$this->fpdf->Cell( 45, 8, "Outlet Name", 1, 0, 'C', true );		
		$this->fpdf->Ln( 8 );
		$this->fpdf->SetFont( 'Arial', '', 8 );
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->Cell( 25, 8, $outlet_id, 1, 0, 'C', $fill );
		$this->fpdf->Cell( 45, 8, $nama_outlet, 1, 0, 'C', $fill );
		
		$this->fpdf->Ln(10);
		$this->fpdf->SetFont('Arial','B',16);		
		$this->fpdf->Write(15, "Return Product");
		
		// Order Information
		$this->fpdf->Ln( 15 );
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 7 );
		$this->fpdf->Cell( 35, 8, "ICCID", 1, 0, 'C', true );
		$this->fpdf->Cell( 25, 8, "Buying Date", 1, 0, 'C', true );		
		$this->fpdf->Cell( 75, 8, "Description", 1, 0, 'C', true );		
		$this->fpdf->Cell( 20, 8, "Price", 1, 0, 'C', true );
		$this->fpdf->Cell( 20, 8, "Status", 1, 0, 'C', true );		
		$this->fpdf->SetFont( 'Arial', '', 7 );
		
		foreach($iccid_valid as $cek){
			$cekid = $cek->iccid;
			$iccid = $cekid;
			$nama = $this->mreturns->getSwapPrintNama($cekid);
			$price = $this->mreturns->getSwapPrintPrice($cekid);
			//$tgl_beli = $this->mreturns->getSwapPrintDate($cekid);
			$str = $this->mreturns->getSwapPrintDate($cekid);
			$tgl_beli = substr($str, 0, 10);
			$state = $this->mreturns->getSwapPrintStatus($cekid, $return_id);
			//$i++;
			
			$this->fpdf->Ln( 8 );
			$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
			$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
			$this->fpdf->Cell( 35, 8, $iccid, 1, 0, 'C', $fill );
			$this->fpdf->Cell( 25, 8, $tgl_beli, 1, 0, 'C', $fill );		
			$this->fpdf->Cell( 75, 8, $nama, 1, 0, 'C', $fill );		
			$this->fpdf->Cell( 20, 8, $price, 1, 0, 'C', $fill );
			$this->fpdf->Cell( 20, 8, $state, 1, 0, 'C', $fill );
		}
		
		  //Position at 1.5 cm from bottom 
        $this->fpdf->SetY(-30); 
        //Arial italic 8 
        $this->fpdf->SetFont('Arial','I',8); 
        //Page number 
        $this->fpdf->Cell(0,10,'Page '.$this->fpdf->PageNo().'/{nb}',0,0,'C'); 
		
		
		$this->fpdf->Output("Return_Swap_Note_".$no.".pdf", "I");
		
    }
	
	function printAuthorized($return_id)
    {
    	$return_id = $this->enc->defog($return_id);
        //$no = $this->getReturnNoString($return_id);
		$no = substr("00000000".$return_id,-8);
		$returns = $this->mreturns->getReturn($return_id);
		$sales_id = $returns->user_id;
		$outlet_id = $returns->channel_id;
		$sales_name = $this->mreturns->getSalesName($sales_id);
		$outlet_name = $this->mreturns->getOutletName($outlet_id);
		
		$datacluster = $this->mreturns->getCluster();
		$cluster = "";
		foreach($datacluster as $item){
			$cluster = $item->territory_name;
		}
		$return_date = $returns->return_date;
		//$data['returns_no'] = $return_id;
		//$data['sales_id'] = $sales_id;
		$nama_sales = $sales_name->user_name;
		//$data['outlet_id'] = $outlet_id;
		$nama_outlet = $outlet_name->outlet_name;
		//$data['remarks'] = $returns->remark;
		
        $iccid_valid = $this->mreturns->getItemsSwapPrint($return_id);
		$iccid = array();
		$nama = array();
		$price = array();
		$tgl_beli = array();
		$state = array();
		$swap = $this->mreturns->getItemsSwap($return_id);
		
		//begin configuration
		$textColour = array( 0, 0, 0 );
		$headerColour = array( 100, 100, 100 );
		$tableHeaderTopTextColour = array( 255, 255, 255 );
		$tableHeaderTopFillColour = array( 125, 152, 179 );
		$tableHeaderTopProductTextColour = array( 0, 0, 0 );
		$tableHeaderTopProductFillColour = array( 143, 173, 204 );
		$tableHeaderLeftTextColour = array( 99, 42, 57 );
		$tableHeaderLeftFillColour = array( 184, 207, 229 );
		$tableBorderColour = array( 50, 50, 50 );
		$tableRowFillColour = array( 213, 170, 170 );
		
		$this->load->helper('path');

		$font_directory = './fpdf_fonts/';
		set_realpath($font_directory);
		define('FPDF_FONTPATH',$font_directory);
		
		$this->fpdf->Open();
		$this->fpdf->AddPage();
		$this->fpdf->SetAutoPageBreak(true, 20);
		$this->fpdf->SetMargins(20, 20, 20);
		$this->fpdf->AliasNbPages();
		
		//header
		$this->fpdf->Image(FCPATH.'file/images/logo_smartfren.png',10,3,50,0,'');		
		$this->fpdf->SetFont('Arial','B',16);
		$this->fpdf->Cell(170,-5,"Return Authorized Notes",0,1,'R');	
		$this->fpdf->Ln( 10 );		
		$this->fpdf->Cell(180,0,"(Pengembalian dan Penukaran Barang)",0,1,'R');		
		$this->fpdf->Line(10, 20, 200, 20);
		
		//table
		$this->fpdf->SetDrawColor( $tableBorderColour[0], $tableBorderColour[1], $tableBorderColour[2] );
		
		//Cluster Information		
		$this->fpdf->Ln(5);
		$this->fpdf->SetFont('Arial','B',16);		
		$this->fpdf->Write(19, $cluster);
		//$this->fpdf->Ln( 6 );
		//$this->fpdf->SetFont('Helvetica','',10);
		//$this->fpdf->Write(19, $dist[0]['distributor_address']);
		
		// Date and receipt no
		$this->fpdf->Ln(15);
		$this->fpdf->SetXY(130, 42);
		$this->fpdf->SetFont('Arial','B',8);
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->Cell( 30, 8, "Date", 1, 0, 'C', true );
		$this->fpdf->Cell( 35, 8, "Return No", 1, 0, 'C', true );		
		//$this->fpdf->Ln( 8 );
		$this->fpdf->SetXY(130, 50);
		
		$fill = false;
		$row = 0;		
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->SetFont( 'Arial', '', 8 );		
		$this->fpdf->Cell( 30, 8, $return_date, 1, 0, 'C', $fill );
		//$this->fpdf->Cell( 25, 8, date("d-m-Y", strtotime($return_date)), 1, 0, 'C', $fill );
		$this->fpdf->Cell( 35, 8, $no, 1, 0, 'C', $fill );
					
		
		$this->fpdf->Ln( 20 );
		
		// Order Information
		$this->fpdf->SetXY(20, 42);
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );
		$this->fpdf->Cell( 25, 8, "Sales ID", 1, 0, 'C', true );
		$this->fpdf->Cell( 45, 8, "Sales Name", 1, 0, 'C', true );		
		$this->fpdf->Ln( 8 );
		$this->fpdf->SetFont( 'Arial', '', 8 );
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->Cell( 25, 8, $sales_id, 1, 0, 'C', $fill );
		$this->fpdf->Cell( 45, 8, $nama_sales, 1, 0, 'C', $fill );

		$this->fpdf->Ln( 25 );
		$this->fpdf->SetXY(20, 62);
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );
		$this->fpdf->Cell( 25, 8, "Outlet ID", 1, 0, 'C', true );
		$this->fpdf->Cell( 45, 8, "Outlet Name", 1, 0, 'C', true );		
		$this->fpdf->Ln( 8 );
		$this->fpdf->SetFont( 'Arial', '', 8 );
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->Cell( 25, 8, $outlet_id, 1, 0, 'C', $fill );
		$this->fpdf->Cell( 45, 8, $nama_outlet, 1, 0, 'C', $fill );
		
		$this->fpdf->Ln(10);
		$this->fpdf->SetFont('Arial','B',16);		
		$this->fpdf->Write(15, "Return Product");
		
		// Order Information
		$this->fpdf->Ln( 15 );
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 7 );
		$this->fpdf->Cell( 35, 8, "ICCID", 1, 0, 'C', true );
		$this->fpdf->Cell( 25, 8, "Buying Date", 1, 0, 'C', true );		
		$this->fpdf->Cell( 75, 8, "Description", 1, 0, 'C', true );		
		$this->fpdf->Cell( 20, 8, "Price", 1, 0, 'C', true );	
		$this->fpdf->Cell( 20, 8, "Status", 1, 0, 'C', true );		
		$this->fpdf->SetFont( 'Arial', '', 7 );
		
		foreach($iccid_valid as $cek){
			$cekid = $cek->iccid;
			$iccid = $cekid;
			$nama = $this->mreturns->getSwapPrintNama($cekid);
			$price = $this->mreturns->getSwapPrintPrice($cekid);
			//$tgl_beli = $this->mreturns->getSwapPrintDate($cekid);
			$str = $this->mreturns->getSwapPrintDate($cekid);
			$tgl_beli = substr($str, 0, 10);
			$state = $this->mreturns->getSwapPrintStatus($cekid, $return_id);
			//$i++;
			
			$this->fpdf->Ln( 8 );
			$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
			$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
			$this->fpdf->Cell( 35, 8, $iccid, 1, 0, 'C', $fill );
			$this->fpdf->Cell( 25, 8, $tgl_beli, 1, 0, 'C', $fill );		
			$this->fpdf->Cell( 75, 8, $nama, 1, 0, 'C', $fill );		
			$this->fpdf->Cell( 20, 8, $price, 1, 0, 'C', $fill );
			$this->fpdf->Cell( 20, 8, $state, 1, 0, 'C', $fill );	
		}
		
		$this->fpdf->Ln(10);
		$this->fpdf->SetFont('Arial','B',16);		
		$this->fpdf->Write(15, "Swap Product");
		
		// Order Information
		$this->fpdf->Ln( 15 );
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 7 );
		$this->fpdf->Cell( 40, 8, "ICCID", 1, 0, 'C', true );
		$this->fpdf->Cell( 40, 8, "Item Code", 1, 0, 'C', true );		
		$this->fpdf->Cell( 60, 8, "Item Name", 1, 0, 'C', true );		
		$this->fpdf->Cell( 35, 8, "Item Group", 1, 0, 'C', true );			
		$this->fpdf->SetFont( 'Arial', '', 7 );
		
		foreach($swap as $cek){
			$iccid = $cek->iccid;
			$code = $cek->item_code;
			$nama = $cek->item_name;
			$grup = $cek->item_group_name;
			
			$this->fpdf->Ln( 8 );
			$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
			$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
			$this->fpdf->Cell( 40, 8, $iccid, 1, 0, 'C', $fill );
			$this->fpdf->Cell( 40, 8, $code, 1, 0, 'C', $fill );		
			$this->fpdf->Cell( 60, 8, $nama, 1, 0, 'C', $fill );		
			$this->fpdf->Cell( 35, 8, $grup, 1, 0, 'C', $fill );	
		}
		
		  //Position at 1.5 cm from bottom 
        $this->fpdf->SetY(-30); 
        //Arial italic 8 
        $this->fpdf->SetFont('Arial','I',8); 
        //Page number 
        $this->fpdf->Cell(0,10,'Page '.$this->fpdf->PageNo().'/{nb}',0,0,'C'); 
		
		
		$this->fpdf->Output("Return_Authorized_Note_".$no.".pdf", "I");
		
    }
	
	function getIccid($term){
        
        $data['iccid'] = $this->mreturns->getIccid($term); //Search DB
        
        if('IS_AJAX')
        {
            echo json_encode($data['iccid']);
        }
		
    }
}
